import loginTpl from '../views/login.art'

const _loginHandle = (router) => {
    return (e) => {
        let username = $('#user-name').val()
        let password = $('#user-pwd').val()
        if( !username || !password ){
            alert("用户名或密码不能为空")
        }else{
            $.ajax({
                url: '/api/user/login',
                method: 'POST',
                data: $('#login-form').serialize(),
                success: function(result) {
                    if( result.status === 200 ){
                        router.go('/index')
                    }else{
                        alert(result.data.message)
                    }
                }
            })
        }
    }
}

export default (router) => {
    return (req, res, next) => {
        next(loginTpl())
        // 挂载登录事件
        $('#login-btn').on('click', _loginHandle(router))
    }
}